<!--
    WasmApp.LocalBuild* files are meant for projects that want to use
    a local build of `dotnet/runtime` repo.

    Two ways to use this:

    1. Use a local runtime build from the source directory:
        Required properties to be set:

        $(RuntimeSrcDir)       - path to the runtime checkout. This should have already been
                                 built, and so should have `artifacts` directory.

        $(RuntimeConfig)       - This is the config that the above checkout was built with.
                                 This can be different from the configuration for the user project.

    2. Use a local directory which has all the tasks, targets, runtimepack available

        Required properties to be set:

        $(WasmBuildSupportDir)
-->
<Project>
  <Import Project="$(_WasmTargetsDir)$(_TargetsBaseName).targets" />

  <UsingTask TaskName="MonoAOTCompiler" AssemblyFile="$(MonoAOTCompilerTasksAssemblyPath)" />
  <UsingTask TaskName="ILStrip" AssemblyFile="$(MonoTargetsTasksAssemblyPath)" />
  <UsingTask TaskName="RuntimeConfigParserTask" AssemblyFile="$(MonoTargetsTasksAssemblyPath)" />

  <PropertyGroup>
    <PublishTrimmed Condition="'$(PublishTrimmed)' == ''">true</PublishTrimmed>
  </PropertyGroup>

  <PropertyGroup Condition="'$(Configuration)' != 'Debug'">
    <!-- Runtime feature defaults to trim unnecessary code -->
    <EventSourceSupport>false</EventSourceSupport>
    <UseSystemResourceKeys>true</UseSystemResourceKeys>
    <EnableUnsafeUTF7Encoding>false</EnableUnsafeUTF7Encoding>
    <HttpActivityPropagationSupport>false</HttpActivityPropagationSupport>
    <DebuggerSupport>false</DebuggerSupport>
    <MetricsSupport>false</MetricsSupport>
  </PropertyGroup>

  <!-- Use local runtime pack -->
  <Target Name="UpdateRuntimePack" AfterTargets="ResolveFrameworkReferences" DependsOnTargets="_PrepareAndValidateWasmInputs">
    <ItemGroup>
      <ResolvedRuntimePack PackageDirectory="$(MicrosoftNetCoreAppRuntimePackLocationToUse)"
                           Condition="'$(MicrosoftNetCoreAppRuntimePackLocationToUse)' != '' and
                                      '%(ResolvedRuntimePack.FrameworkName)' == 'Microsoft.NETCore.App'" />
    </ItemGroup>
    <Message Text="Used runtime pack: %(ResolvedRuntimePack.PackageDirectory) for $(MSBuildProjectName)" Importance="normal" />
  </Target>

  <!-- the actual properties need to get set in the props, so because UsingTasks depend on those. -->
  <Target Name="_PrepareAndValidateWasmInputs">
    <Error Condition="'$(RuntimeSrcDir)' == '' and '$(WasmBuildSupportDir)' == ''"
           Text="Both %24(RuntimeSrcDir) and %24(WasmBuildSupportDir) are not set. Either one of them needs to be set to use local runtime builds" />

    <Error Condition="'$(RuntimeSrcDir)' != '' and '$(WasmBuildSupportDir)' != ''"
           Text="Both %24(RuntimeSrcDir) and %24(WasmBuildSupportDir) are set. " />

    <Error Condition="'$(RuntimeSrcDir)' != '' and '$(RuntimeConfig)' == ''"
           Text="%24(RuntimeSrcDir) is set, but %24(RuntimeConfig) is not" />

    <PropertyGroup>
      <MicrosoftNetCoreAppRuntimePackDir>$([MSBuild]::NormalizeDirectory($(MicrosoftNetCoreAppRuntimePackLocationToUse)))</MicrosoftNetCoreAppRuntimePackDir>
      <MicrosoftNetCoreAppRuntimePackRidDir>$([MSBuild]::NormalizeDirectory($(MicrosoftNetCoreAppRuntimePackDir), 'runtimes', $(RuntimeIdentifier)))</MicrosoftNetCoreAppRuntimePackRidDir>
      <WasmAppBuilderTasksAssemblyPath>$([MSBuild]::NormalizePath('$(WasmAppBuilderDir)', 'WasmAppBuilder.dll'))</WasmAppBuilderTasksAssemblyPath>
      <WasmBuildTasksAssemblyPath>$([MSBuild]::NormalizePath('$(WasmBuildTasksDir)', 'WasmBuildTasks.dll'))</WasmBuildTasksAssemblyPath>
      <MonoAOTCompilerTasksAssemblyPath>$([MSBuild]::NormalizePath('$(MonoAOTCompilerDir)', 'MonoAOTCompiler.dll'))</MonoAOTCompilerTasksAssemblyPath>
      <MonoTargetsTasksAssemblyPath>$([MSBuild]::NormalizePath('$(MonoTargetsTasksDir)', 'MonoTargetsTasks.dll'))</MonoTargetsTasksAssemblyPath>
    </PropertyGroup>

    <Error Condition="!Exists('$(MicrosoftNetCoreAppRuntimePackRidDir)')"
           Text="Cannot find runtime pack at $(MicrosoftNetCoreAppRuntimePackRidDir). Make sure you have the correct %24(RuntimeConfig) set" />

    <Warning
      Condition="'$(WasmMainJS)' != '' and '$(WasmGenerateAppBundle)' != 'true'"
      Text="%24(WasmMainJS) is set when %24(WasmGenerateAppBundle) is not true: it won't be used because an app bundle is not being generated. Possible build authoring error" />
  </Target>
</Project>
